﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.Text;
using Tickz.Commonz;

namespace Tickz.Configz
{
    /// <summary>
    /// 文件配置管理基类
    /// </summary>
    public class DefaultConfigFileManagerz : IConfigFileManagerz
    {
        #region 字段区
            /// <summary>
            /// 文件所在路径变量
            /// </summary>
            private static string _configFilePath;

            /// <summary>
            /// 临时配置对象变量
            /// </summary>
            private static IConfigInfoz _configInfo = null;

            /// <summary>
            /// 锁对象
            /// </summary>
            private static readonly object LockHelper = new object(); 
        #endregion


         #region 属性区
            /// <summary>
            /// 文件所在路径
            /// </summary>
            public static string ConfigFilePath
            {
                get { return _configFilePath; }
                set { _configFilePath = value; }
            }


            /// <summary>
            /// 临时配置对象
            /// </summary>
            public static IConfigInfoz ConfigInfo
            {
                get { return _configInfo; }
                set { _configInfo = value; }
            }
            #endregion

            #region 获得配置对象
            /// <summary>
            /// 始终返回null
            /// </summary>
            /// <returns></returns>
            public IConfigInfoz LoadConfig()
            {
                return null;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="configFilePath"></param>
            /// <param name="configinfo"></param>
            /// <returns></returns>
            protected static IConfigInfoz LoadConfig(string configFilePath, IConfigInfoz configinfo)
            {
                return DeserializeInfo(configFilePath, configinfo.GetType());
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="configFilePath"></param>
            /// <param name="configinfo"></param>
            /// <returns></returns>
            protected static IConfigInfoz LoadConfig(string configFilePath, Type type)
            {
                return DeserializeInfo(configFilePath, type);
            }
            /// <summary>
            /// 加载(反序列化)指定对象类型的配置对象
            /// </summary>
            /// <param name="fileoldchange">文件加载时间</param>
            /// <param name="configFilePath">配置文件所在路径</param>
            /// <param name="configinfo">相应的变量 注:该参数主要用于设置configinfo变量 和 获取类型.GetType()</param>
            /// <returns></returns>
            protected static IConfigInfoz LoadConfig(ref DateTime fileoldchange, string configFilePath, IConfigInfoz configinfo)
            {
                return LoadConfig(ref fileoldchange, configFilePath, configinfo, true);
            }


            /// <summary>
            /// 加载(反序列化)指定对象类型的配置对象
            /// </summary>
            /// <param name="fileoldchange">文件加载时间</param>
            /// <param name="configFilePath">配置文件所在路径(包括文件名)</param>
            /// <param name="configinfo">相应的变量 注:该参数主要用于设置m_configinfo变量 和 获取类型.GetType()</param>
            /// <param name="checkTime">是否检查并更新传递进来的"文件加载时间"变量</param>
            /// <returns></returns>
            public static IConfigInfoz LoadConfig(ref DateTime fileoldchange, string configFilePath, IConfigInfoz configinfo, bool checkTime)
            {
                lock (LockHelper)
                {
                    ConfigFilePath = configFilePath;
                    ConfigInfo = configinfo;
                    if (checkTime)
                    {
                        DateTime filenewchange = System.IO.File.GetLastWriteTime(configFilePath);
                        //当程序运行中config文件发生变化时则对config重新赋值
                        if (fileoldchange != filenewchange)
                        {
                            fileoldchange = filenewchange;
                            ConfigInfo = DeserializeInfo(configFilePath, configinfo.GetType());
                        }
                    }
                    else
                        ConfigInfo = DeserializeInfo(configFilePath, configinfo.GetType());

                    return ConfigInfo;
                }
            }
            /// <summary>
            /// 反序列化指定的类
            /// </summary>
            /// <param name="configfilepath">config 文件的路径</param>
            /// <param name="configtype">相应的类型</param>
            /// <returns></returns>
            private static IConfigInfoz DeserializeInfo(string configfilepath, Type configtype)
            {
                return (IConfigInfoz)SerializationHelper.Load(configtype, configfilepath);
            } 
            #endregion




            #region 保存对象
            /// <summary>
            /// 保存配置实例(虚方法需继承)  始终返回true
            /// </summary>
            /// <returns></returns>
            public virtual bool SaveConfig()
            {
                return true;
            }

            /// <summary>
            /// 保存(序列化)指定路径下的配置文件
            /// </summary>
            /// <param name="configFilePath">指定的配置文件所在的路径(包括文件名) 绝对的路径</param>
            /// <param name="configinfo">被保存(序列化)的对象</param>
            /// <returns></returns>
            protected static bool SaveConfig(string configFilePath, IConfigInfoz configinfo)
            {
                return SerializationHelper.Save(configinfo, configFilePath);
            } 
            #endregion
    }
}
